package com.ityedao.String案例;

import java.util.Random;

public class VerificationCode {
    public static void main(String[] args) {
        System.out.println(createCode(4));
        System.out.println(createCode(7));
    }

    /**
     * 返回指定位数的验证码
     * @param n 验证码位数
     * @return
     */
    public static String createCode(int n) {
        String code = "";
        String data = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

        Random r = new Random();
        for (int i = 0; i < n; i ++) {
            int index = r.nextInt(data.length());
            code += data.charAt(index);
        }
        return code;
    }

}
